/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RobotSelector.java
 *
 * Created on Feb 14, 2009, 10:12:50 AM
 */

package gears.editors;

import javax.swing.RowFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;
import gears.controllers.RobotController;
import gears.entities.Robot;

/**
 *
 * @author duo
 */
public class RobotSelector extends javax.swing.JDialog {

    protected RobotController robotController;
    protected Robot selectedRobot;

    /** Creates new form RobotSelector */
    public RobotSelector(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        robotController = new RobotController();
        robotTable.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {

            @Override
                    public void valueChanged(ListSelectionEvent e) {
                        firePropertyChange("robotRecordSelected",
                                !isRobotRecordSelected(), isRobotRecordSelected());
                    }
                });
    }

    public Robot getSelectedRobot() {
        return selectedRobot;
    }

    public void setSelectedRobot(Robot selectedRobot) {
        this.selectedRobot = selectedRobot;
    }

    public boolean isRobotRecordSelected() {
        return robotTable.getSelectedRow() != -1;
    }

    @Action
    public void newRobot() {
        robotController.newRobot(gearsPUEntityManager, robotList);
    }

    @Action(enabledProperty = "robotRecordSelected")
    public void editRobot() {
        int index = robotTable.getSelectedRow();
        robotController.setRobot(robotList.get(robotTable.
                convertRowIndexToModel(index)));
        robotController.editRobot(gearsPUEntityManager, robotList,
                robotTable.convertRowIndexToModel(index));
    }

    @Action(enabledProperty = "robotRecordSelected")
    public void deleteRobot() {
        int index = robotTable.getSelectedRow();
        robotController.setRobot(robotList.get(robotTable.
            convertRowIndexToModel(index)));
        robotController.deleteRobot(gearsPUEntityManager, robotList,
            robotTable.convertRowIndexToModel(index));
    }

    @Action
    public void cancelRobotSelector() {
        setSelectedRobot(null);
        setVisible(false);
    }

    @Action(enabledProperty = "robotRecordSelected")
    public void selectRobot() {
        int index = robotTable.getSelectedRow();
        setSelectedRobot(robotList.get(robotTable.
                convertRowIndexToModel(index)));
        setVisible(false);
    }

    @Action
    public void filterRobot() {
        TableRowSorter robotSorter = (TableRowSorter) robotTable.getRowSorter();
        String text = robotFilterTextField.getText();
        robotFilterTextField.setText("");
        if(text.length()==0) {
            robotSorter.setRowFilter(null);
        } else {
            robotSorter.setRowFilter(RowFilter.regexFilter("(?u)(?i)" + text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        gearsPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("gearsPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gears.GearsApp.class).getContext().getResourceMap(RobotSelector.class);
        robotQuery = java.beans.Beans.isDesignTime() ? null : gearsPUEntityManager.createQuery(resourceMap.getString("robotQuery.query")); // NOI18N
        robotList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(robotQuery.getResultList());
        robotFilterTextField = new javax.swing.JTextField();
        robotFilterButton = new javax.swing.JButton();
        robotSelectionPanel = new javax.swing.JPanel();
        robotScrollPane = new javax.swing.JScrollPane();
        robotTable = new javax.swing.JTable();
        newButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        robotFilterTextField.setText(resourceMap.getString("robotFilterTextField.text")); // NOI18N
        robotFilterTextField.setName("robotFilterTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gears.GearsApp.class).getContext().getActionMap(RobotSelector.class, this);
        robotFilterButton.setAction(actionMap.get("filterRobot")); // NOI18N
        robotFilterButton.setText(resourceMap.getString("robotFilterButton.text")); // NOI18N
        robotFilterButton.setName("robotFilterButton"); // NOI18N

        robotSelectionPanel.setName("robotSelectionPanel"); // NOI18N

        robotScrollPane.setName("robotScrollPane"); // NOI18N

        robotTable.setAutoCreateRowSorter(true);
        robotTable.setName("robotTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, robotList, robotTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${name}"));
        columnBinding.setColumnName("Name");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${series}"));
        columnBinding.setColumnName("Series");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        robotScrollPane.setViewportView(robotTable);

        javax.swing.GroupLayout robotSelectionPanelLayout = new javax.swing.GroupLayout(robotSelectionPanel);
        robotSelectionPanel.setLayout(robotSelectionPanelLayout);
        robotSelectionPanelLayout.setHorizontalGroup(
            robotSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 423, Short.MAX_VALUE)
            .addGroup(robotSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(robotSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(robotScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        robotSelectionPanelLayout.setVerticalGroup(
            robotSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 170, Short.MAX_VALUE)
            .addGroup(robotSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(robotSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(robotScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        newButton.setAction(actionMap.get("newRobot")); // NOI18N
        newButton.setText(resourceMap.getString("newButton.text")); // NOI18N
        newButton.setName("newButton"); // NOI18N

        editButton.setAction(actionMap.get("editRobot")); // NOI18N
        editButton.setText(resourceMap.getString("editButton.text")); // NOI18N
        editButton.setName("editButton"); // NOI18N

        deleteButton.setAction(actionMap.get("deleteRobot")); // NOI18N
        deleteButton.setText(resourceMap.getString("deleteButton.text")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N

        cancelButton.setAction(actionMap.get("cancelRobotSelector")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(robotFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(robotFilterButton))
                    .addComponent(robotSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newButton)
                        .addGap(18, 18, 18)
                        .addComponent(editButton)
                        .addGap(18, 18, 18)
                        .addComponent(deleteButton)
                        .addGap(18, 18, 18)
                        .addComponent(cancelButton)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(robotFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(robotFilterButton))
                .addGap(27, 27, 27)
                .addComponent(robotSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newButton)
                    .addComponent(editButton)
                    .addComponent(deleteButton)
                    .addComponent(cancelButton))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                RobotSelector dialog = new RobotSelector(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton editButton;
    private javax.persistence.EntityManager gearsPUEntityManager;
    private javax.swing.JButton newButton;
    private javax.swing.JButton robotFilterButton;
    private javax.swing.JTextField robotFilterTextField;
    private java.util.List<gears.entities.Robot> robotList;
    private javax.persistence.Query robotQuery;
    private javax.swing.JScrollPane robotScrollPane;
    private javax.swing.JPanel robotSelectionPanel;
    private javax.swing.JTable robotTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
